package cn.iocoder.yudao.module.fm.controller.admin.tunnel;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.fm.controller.admin.tunnel.vo.*;
import cn.iocoder.yudao.module.fm.convert.tunnel.TunnelConvert;
import cn.iocoder.yudao.module.fm.dal.dataobject.tunnel.TunnelDO;
import cn.iocoder.yudao.module.fm.enums.FileInfoType;
import cn.iocoder.yudao.module.fm.service.fileinfo.FileInfoService;
import cn.iocoder.yudao.module.fm.service.tunnel.TunnelService;
import cn.iocoder.yudao.module.fm.utils.DownloadTemplateUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 隧道")
@RestController
@RequestMapping("/fm/tunnel")
@Validated
public class TunnelController {

    @Resource
    private TunnelService tunnelService;
    @Resource
    private FileInfoService fileInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建隧道")
    @PreAuthorize("@ss.hasPermission('fm:tunnel:create')")
    public CommonResult<Long> createTunnel(@Valid @RequestBody TunnelCreateReqVO createReqVO) {
        return success(tunnelService.createTunnel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新隧道")
    @PreAuthorize("@ss.hasPermission('fm:tunnel:update')")
    public CommonResult<Boolean> updateTunnel(@Valid @RequestBody TunnelUpdateReqVO updateReqVO) {
        tunnelService.updateTunnel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除隧道")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fm:tunnel:delete')")
    public CommonResult<Boolean> deleteTunnel(@RequestParam("id") Long id) {
        tunnelService.deleteTunnel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得隧道")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fm:tunnel:query')")
    public CommonResult<TunnelRespVO> getTunnel(@RequestParam("id") Long id) {
        TunnelDO tunnel = tunnelService.getTunnel(id);
        TunnelRespVO tunnelRespVO = TunnelConvert.INSTANCE.convert(tunnel);
        tunnelRespVO.setFileList(fileInfoService.get(tunnelRespVO.getId(), FileInfoType.TUNNEL.getValue()));

        return success(tunnelRespVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得隧道列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fm:tunnel:query')")
    public CommonResult<List<TunnelRespVO>> getTunnelList(@RequestParam("ids") Collection<Long> ids) {
        List<TunnelDO> list = tunnelService.getTunnelList(ids);
        return success(TunnelConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得隧道分页")
    @PreAuthorize("@ss.hasPermission('fm:tunnel:query')")
    public CommonResult<PageResult<TunnelRespVO>> getTunnelPage(@Valid TunnelPageReqVO pageVO) {
        PageResult<TunnelDO> pageResult = tunnelService.getTunnelPage(pageVO);
        return success(TunnelConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出隧道 Excel")
    @PreAuthorize("@ss.hasPermission('fm:tunnel:export')")
    @OperateLog(type = EXPORT)
    public void exportTunnelExcel(@Valid TunnelExportReqVO exportReqVO,
                                  HttpServletResponse response) throws IOException {
        List<TunnelDO> list = tunnelService.getTunnelList(exportReqVO);
        // 导出 Excel
        List<TunnelExcelVO> datas = TunnelConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "隧道.xls", "数据", TunnelExcelVO.class, datas);
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入模板")
    public ResponseEntity<byte[]> importTemplate() {
        return DownloadTemplateUtil.downloadTemplate("tunnelTemplate.xlsx", "template/tunnel-export-template-download.xlsx");
    }

    @PostMapping("/import")
    @Operation(summary = "导入")
    @PreAuthorize("@ss.hasPermission('system:tunnel:import')")
    public CommonResult<Boolean> importExcel(@RequestParam("file") MultipartFile file) {
        tunnelService.importExcel(file);
        return CommonResult.success(true);
    }

    @GetMapping("/export")
    @Operation(summary = "导出")
    @PreAuthorize("@ss.hasPermission('system:tunnel:export')")
    public ResponseEntity<byte[]> exportExcel(Long id) {
        return tunnelService.exportExcel(getTunnel(id).getData());
    }

}
